package app

import (
	"bytes"
	"log"
	"os/exec"
	"strings"
)

func ExecScripts(scripts []string) error {
	var stdin bytes.Buffer
	var stdout bytes.Buffer
	var stderr bytes.Buffer

	cmd := exec.Command("sh")
	cmd.Stdin = &stdin
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr

	go func() {
		//in.WriteString("echo hello world >> test.txt\n")
		//in.WriteString("exit 10\n")
		////in.WriteString("sleep 5\n")
		stdin.WriteString(strings.Join(scripts, "\n"))
	}()

	err := cmd.Run()
	for i := range scripts {
		log.Printf("----------Run Script: %s------------------", scripts[i])
	}
	log.Printf("\nstdout: %s \nstderr: %s", stdout.String(), stderr.String())

	if err != nil {
		log.Printf("exec script error %v", err)
		return err
	}
	return nil
}
